﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MasterDetailTemplate.Models;
using MasterDetailTemplate.Services;
using MvvmHelpers;

namespace MasterDetailTemplate.ViewModels {
    /// <summary>
    /// 添加事件。
    /// By XJ
    /// </summary>
    public class AddEventViewModel : ViewModelBase {
        /// <summary>
        /// 事件存储。
        /// </summary>
        private IEventStorage _eventStorage;

        /// <summary>
        /// 根导航服务。
        /// </summary>
        private IRootNavigationService _rootNavigationService;

        /// <summary>
        /// 设置倒数日时最小日期。
        /// </summary>
        public DateTime Today { get; set; }

        /// <summary>
        /// 事件种类1。
        /// </summary>
        public List<string> Attributelist1 { get; set; }

        /// <summary>
        /// 事件种类2。
        /// </summary>
        public List<string> Attributelist2 { get; set; }

        /// <summary>
        /// 要添加的事件。
        /// </summary>
        public Event Events
        {
            get => _event;
            set => Set(nameof(Events), ref _event, value);
        }

        private Event _event;

        /// <summary>
        /// 事件的种类
        /// </summary>
        public string Type
        {
            get => _type;
            set => Set(nameof(Type), ref _type, value);
        }

        private string _type;

        /// <summary>
        /// 事件是否置顶。
        /// </summary>
        public bool isImport
        {
            get => _isImport;
            set => Set(nameof(isImport), ref _isImport, value);
        }

        private bool _isImport;

        /// <summary>
        /// 页面显示命令。
        /// </summary>
        private RelayCommand _pageAppearingCommand;

        /// <summary>
        /// 页面显示命令。
        /// </summary>
        public RelayCommand PageAppearingCommand =>
            _pageAppearingCommand ?? (_pageAppearingCommand =
                new RelayCommand(async () => {
                    await PageAppearingCommandFunction();
                }));

        public async Task PageAppearingCommandFunction()
        {
            Type = "";
            isImport = false;
            Events = new Event {Title = "",Date = DateTime.Now };
        }

        /// <summary>
        /// 提交日期内容。
        /// </summary>
        private RelayCommand _submitCommand;

        /// <summary>
        /// 提交日期内容。
        /// </summary>
        public RelayCommand SubmitCommand =>
            _submitCommand ?? (_submitCommand = new RelayCommand(async () => {
                await SubmitCommandFunction();
            }));

        public async Task SubmitCommandFunction()
        {
            // isImport = true;
            // Type = "纪念日";
            Events.Id = DateTime.Now.Ticks;
            Events.IsImportant = isImport ? 1 : 0;
            Events.Attribute = Type.Equals("纪念日") ? 1 : 2;
            Events.DateStamp = DateTime.Now.Ticks;
            Events.Date = Events.Date.Date;
            /*await _eventStorage.InitializeAsync();*/
            await _eventStorage.InsertEventAsync(Events);
            await _rootNavigationService.NavigateToAsync(
                RootNavigationServiceConstants.HomePage);
        }

        /// <summary>
        /// 添加事件。
        /// </summary>
        /// <param name="eventStorage">事件存储。</param>
        public AddEventViewModel(IEventStorage eventStorage,
            IRootNavigationService rootNavigationService) {
            _eventStorage = eventStorage;
            _rootNavigationService = rootNavigationService;
            //Events = new Event {Title="111", Date = DateTime.Now};
            Today = DateTime.Now;
            Attributelist1 = new List<string> {"纪念日", "倒数日"};
            Attributelist2 = new List<string> {"纪念日"};
        }
    }
}